package com.zego.wawaji.ui.coupon.dialog;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.coupon.bean.Coupon;

import java.util.List;

/**
 * Created by Su on 2018/2/26.
 */
public class CouponBottimAdapter extends ExBaseAdapter<Coupon> {
    private ImageView lastChoose;
    private OnCouponChooseListener listener;

    public CouponBottimAdapter(Context context, List<Coupon> datas) {
        super(context, datas);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.coupon_dialog_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final Coupon data = getItem(position);

        holder.de_type.setText(data.getRemark());
        holder.coupon_item.setOnClickListener(v -> {
            if (lastChoose != null) {
                lastChoose.setSelected(false);
            }
            if (holder.iv_choose.isSelected()) {
                holder.iv_choose.setSelected(false);
            } else {
                holder.iv_choose.setSelected(true);
            }
            lastChoose = holder.iv_choose;

            if (listener != null) {
                listener.onClick(data, position);
            }
        });

        return convertView;
    }

    private class MyViewHolder {
        private ImageView iv_choose;
        private RelativeLayout coupon_item;
        private TextView de_type;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            de_type = (TextView) convertView.findViewById(R.id.de_type);
            coupon_item = (RelativeLayout) convertView.findViewById(R.id.coupon_item);
            iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
        }
    }

    public void setOnChooseListener(OnCouponChooseListener listener) {
        this.listener = listener;
    }
}
